system-controller: initial support for disconnecting homescreen clients.
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 if not m:plugin_exists('ivi-resource-manager') and
239    not with_system_controller
240 then
241     resource.method.veto = {
242         function(zone, rset, grant, owners, req_set)
243             return true
244          end
245     }
246 end
247
248 -- test for creating selections
249 mdb.select {
250            name = "audio_owner",
251            table = "audio_playback_owner",
252            columns = {"application_class"},
253            condition = "zone_name = 'driver'"
254 }
255
256 mdb.select {
257            name = "vehicle_speed",
258            table = "amb_vehicle_speed",
259            columns = {"value"},
260            condition = "key = 'VehicleSpeed'"
261 }
262
263 element.lua {
264    name    = "speed2volume",
265    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
266    outputs = {  mdb.table { name = "speedvol",
267                             index = {"zone", "device"},
268                             columns = {{"zone", mdb.string, 16},
269                                        {"device", mdb.string, 16},
270                                        {"value", mdb.floating}},
271                             create = true
272                            }
273              },
274    oldvolume = 0.0,
275    update  = function(self)
276                 speed = self.inputs.speed.single_value
277                 if (speed) then
278                     volume = (speed - 144.0) / 7.0
279                 else
280                     volume = 0.0
281                 end
282                 diff = volume - self.oldvolume
283                 if (diff*diff > self.inputs.param) then
284                     print("*** element "..self.name.." update "..volume)
285                     self.oldvolume = volume
286                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
287                 end
288              end
289 }
290
291 mdb.select {
292     name = "amb_state",
293     table = "amb_state",
294     columns = { "state" },
295     condition = "id = 0"
296 }
297
298 -- Night mode processing chain
299
300 mdb.select {
301     name = "exterior_brightness",
302     table = "amb_exterior_brightness",
303     columns = { "value" },
304     condition = "key = 'ExteriorBrightness'"
305 }
306
307 element.lua {
308     name    = "nightmode",
309     inputs  = { brightness = mdb.select.exterior_brightness },
310     oldmode = -1;
311     outputs = {
312         mdb.table {
313             name = "amb_nightmode",
314             index = { "id" },
315             create = true,
316             columns = {
317                 { "id", mdb.unsigned },
318                 { "night_mode", mdb.unsigned }
319             }
320         }
321     },
322     update = function(self)
323         -- This is a trivial function to calculate night mode. Later, we will
324         -- need a better threshold value and hysteresis to prevent oscillation.
325
326         brightness = self.inputs.brightness.single_value
327
328         if not brightness then
329             return
330         end
331
332         print("*** element "..self.name.." update brightness: "..brightness)
333
334         if brightness > 300 then
335             mode = 0
336         else
337             mode = 1
338         end
339
340         print("*** resulting mode: ".. mode)
341
342         if not (mode == self.oldmode) then
343             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
344         end
345
346         self.oldmode = mode
347     end
348 }
349
350 mdb.select {
351     name = "select_night_mode",
352     table = "amb_nightmode",
353     columns = { "night_mode" },
354     condition = "id = 0"
355 }
356
357 if with_amb then
358     sink.lua {
359         name = "night_mode",
360         inputs = { NightMode = mdb.select.select_night_mode,
361                    amb_state = mdb.select.amb_state },
362         property = "NightMode",
363         type = "b",
364         initiate = builtin.method.amb_initiate,
365         update = builtin.method.amb_update
366     }
367 end
368
369 -- Night mode general handlers
370
371 sink.lua {
372     name = "nightmode_homescreen",
373     inputs = { owner = mdb.select.select_night_mode },
374     initiate = function(self)
375                    -- data = mdb.select.select_night_mode.single_value
376                    return true
377                end,
378     update = function(self)
379                  send_night_mode_to_home_screen()
380              end
381 }
382
383 -- Driving mode processing chain
384
385 element.lua {
386     name    = "drivingmode",
387     inputs  = { speed = mdb.select.vehicle_speed },
388     oldmode = -1;
389     outputs = {
390         mdb.table {
391             name = "amb_drivingmode",
392             index = { "id" },
393             create = true,
394             columns = {
395                 { "id", mdb.unsigned },
396                 { "driving_mode", mdb.unsigned }
397             }
398         }
399     },
400     update = function(self)
401
402         speed = self.inputs.speed.single_value
403
404         if not speed then
405             return
406         end
407
408         if speed == 0 then
409             mode = 0
410         else
411             mode = 1
412         end
413
414         if not (mode == self.oldmode) then
415             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
416         end
417
418         self.oldmode = mode
419     end
420 }
421
422 mdb.select {
423     name = "select_driving_mode",
424     table = "amb_drivingmode",
425     columns = { "driving_mode" },
426     condition = "id = 0"
427 }
428
429 if with_amb then
430     sink.lua {
431         name = "driving_mode",
432         inputs = { DrivingMode = mdb.select.select_driving_mode,
433                    amb_state = mdb.select.amb_state },
434         property = "DrivingMode",
435         type = "u",
436         initiate = builtin.method.amb_initiate,
437         update = builtin.method.amb_update
438     }
439 end
440
441 -- turn signals (left, right)
442
443 mdb.select {
444     name = "winker",
445     table = "amb_turn_signal",
446     columns = { "value" },
447     condition = "key = 'TurnSignal'"
448 }
449
450 -- define three categories
451
452 mdb.select {
453     name = "undefined_applications",
454     table = "aul_applications",
455     columns = { "appid" },
456     condition = "category = '<undefined>'"
457 }
458
459 mdb.select {
460     name = "basic_applications",
461     table = "aul_applications",
462     columns = { "appid" },
463     condition = "category = 'basic'"
464 }
465
466 mdb.select {
467     name = "entertainment_applications",
468     table = "aul_applications",
469     columns = { "appid" },
470     condition = "category = 'entertainment'"
471 }
472
473 function ft(t)
474     -- filter the object garbage out of the tables
475     ret = {}
476
477     for k,v in pairs(t) do
478         if k ~= "userdata" and k ~= "new" then
479             ret[k] = v
480         end
481     end
482
483     return ret
484 end
485
486 function getApplication(appid)
487     local conf = nil
488
489     -- find the correct local application definition
490
491     for k,v in pairs(ft(application)) do
492         if appid == v.appid then
493             conf = v
494             break
495         end
496     end
497
498     return conf
499 end
500
501 function regulateApplications(t, regulation)
502     for k,v in pairs(ft(t)) do
503
504         -- iterate through the undefined and entertainment apps, see if
505         -- they have been overruled in local config
506
507         local conf = getApplication(v.appid)
508
509         if conf and conf.resource_class ~= "player" then
510             -- override, don't disable
511             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
512         else
513             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
514         end
515     end
516     resource.method.recalc("driver")
517 end
518
519 -- regulation (on), use "select_driving_mode"
520
521 sink.lua {
522     name = "driving_regulation",
523     inputs = { owner = mdb.select.select_driving_mode },
524     initiate = function(self)
525         -- local data = mdb.select.select_driving_mode.single_value
526         return true
527     end,
528     update = function(self)
529         local data = mdb.select.select_driving_mode.single_value
530
531         if verbose > 1 then
532             print("Driving mode updated: " .. tostring(data))
533         end
534
535         if not sc then
536             return true
537         end
538
539         -- tell homescreen that driving mode was updated
540         send_driving_mode_to_home_screen()
541
542         regulateApplications(ft(mdb.select.entertainment_applications), data)
543         regulateApplications(ft(mdb.select.undefined_applications), data)
544
545         return true
546     end
547 }
548 --[[
549 sink.lua {
550     name = "regulated_app_change",
551     inputs = { undef = mdb.select.undefined_applications,
552                entertainment = mdb.select.entertainment_applications },
553     initiate = function(self)
554         return true
555     end,
556     update = function(self)
557         local data = mdb.select.select_driving_mode.single_value
558
559         if not sc then
560             return
561         end
562
563         if verbose > 1 then
564             print("regulated application list was changed")
565         end
566
567         regulateApplications(ft(mdb.select.entertainment_applications), data)
568         regulateApplications(ft(mdb.select.undefined_applications), data)
569
570         return true
571     end
572 }
573 --]]
574
575 -- shift position (parking, reverse, other)
576
577 mdb.select {
578     name = "gear_position",
579     table = "amb_gear_position",
580     columns = { "value" },
581     condition = "key = 'GearPosition'"
582 }
583
584 -- cameras (back, front, left, right)
585
586 element.lua {
587     name    = "camera_state",
588     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
589     oldmode = -1;
590     outputs = {
591         mdb.table {
592             name = "target_camera_state",
593             index = { "id" },
594             create = true,
595             columns = {
596                 { "id", mdb.unsigned },
597                 { "front_camera", mdb.unsigned },
598                 { "back_camera", mdb.unsigned },
599                 { "right_camera", mdb.unsigned },
600                 { "left_camera", mdb.unsigned }
601             }
602         }
603     },
604     update = function(self)
605
606         front_camera = 0
607         back_camera = 0
608         right_camera = 0
609         left_camera = 0
610
611         if self.inputs.gear == 128 then
612             back_camera = 1
613         elseif self.inputs.winker == 1 then
614             right_camera = 1
615         elseif self.inputs.winker == 2 then
616             left_camera = 1
617         end
618
619         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
620
621     end
622 }
623
624 -- system controller test setup
625
626 if not with_system_controller then
627    -- ok, we should have 'audio_playback' defined by now
628    m:try_load_plugin('telephony')
629    return
630 end
631
632 m:load_plugin('system-controller')
633
634 window_manager_operation_names = {
635     [1] = "create",
636     [2] = "destroy"
637 }
638
639 function window_manager_operation_name(oper)
640     local name = window_manager_operation_names[oper]
641     if name then return name end
642     return "<unknown " .. tostring(oper) .. ">"
643 end
644
645 window_operation_names = {
646     [1] = "create",
647     [2] = "destroy",
648     [3] = "name_change",
649     [4] = "visible",
650     [5] = "configure",
651     [6] = "active"
652 }
653
654 function window_operation_name(oper)
655     local name = window_operation_names[oper]
656     if name then return name end
657     return "<unknown " .. tostring(oper) .. ">"
658 end
659
660 layer_operation_names = {
661     [1] = "create",
662     [2] = "destroy",
663     [3] = "visible"
664 }
665
666 function layer_operation_name(oper)
667     local name = layer_operation_names[oper]
668     if name then return name end
669     return "<unknown " .. tostring(oper) .. ">"
670 end
671
672 input_manager_operation_names = {
673     [1] = "create",
674     [2] = "destroy",
675     [3] = "ready"
676 }
677
678 function input_manager_operation_name(oper)
679     local name = input_manager_operation_names[oper]
680     if name then return name end
681     return "<unknown " .. tostring(oper) .. ">"
682 end
683
684 input_operation_names = {
685     [1] = "create",
686     [2] = "destroy",
687     [3] = "update"
688 }
689
690 function input_operation_name(oper)
691     local name = input_operation_names[oper]
692     if name then return name end
693     return "<unknown " .. tostring(oper) .. ">"
694 end
695
696 code_operation_names = {
697     [1] = "create",
698     [2] = "destroy",
699     [3] = "state_change"
700 }
701
702 function code_operation_name(oper)
703     local name = code_operation_names[oper]
704     if name then return name end
705     return "<unknown " .. tostring(oper) .. ">"
706 end
707
708 command_names = {
709     [0x00001] = "send_appid",
710     [0x10001] = "create",
711     [0x10002] = "destroy",
712     [0x10003] = "show",
713     [0x10004] = "hide",
714     [0x10005] = "move",
715     [0x10006] = "change_active",
716     [0x10007] = "change_layer",
717     [0x10008] = "change_attr",
718     [0x10009] = "name",
719     [0x10011] = "map_thumb",
720     [0x10012] = "unmap_thumb",
721     [0x10020] = "show layer",
722     [0x10021] = "hide_layer",
723     [0x10022] = "change_layer_attr",
724     [0x20001] = "add_input",
725     [0x20002] = "del_input",
726     [0x20003] = "send_input",
727     [0x40001] = "acquire_res",
728     [0x40002] = "release_res",
729     [0x40003] = "deprive_res",
730     [0x40004] = "waiting_res",
731     [0x40005] = "revert_res",
732     [0x40011] = "create_res",
733     [0x40012] = "destroy_res",
734     [0x50001] = "set_region",
735     [0x50002] = "unset_region",
736     [0x60001] = "change_state"
737 }
738
739 function command_name(command)
740     local name = command_names[command]
741     if name then return name end
742     return "<unknown " .. tostring(command) .. ">"
743 end
744
745 input_layer = {
746    [3] = true, -- input
747    [4] = true, -- touch
748    [5] = true  -- cursor
749 }
750
751 resmgr = resource_manager {
752   screen_event_handler = function(self, ev)
753                              local event = ev.event
754                              local surface = ev.surface
755
756                              if event == "init" then
757                                  if verbose > 0 then
758                                      print("*** init screen resource allocation -- disable all 'player'")
759                                  end
760                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
761                              elseif event == "preallocate" then
762                                  if verbose > 0 then
763                                      print("*** preallocate screen resource "..
764                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
765                                  end
766                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
767                              elseif event == "grant" then
768                                  if verbose > 0 then
769                                     print("*** make visible surface "..surface)
770                                  end
771                                  local a = animation({})
772                                  local r = m:JSON({surface = surface,
773                                                    visible = 1,
774                                                    raise   = 1})
775                                  wmgr:window_request(r,a,0)
776                              elseif event == "revoke" then
777                                  if verbose > 0 then
778                                     print("*** hide surface "..surface)
779                                  end
780                                  local a = animation({})
781                                  local r = m:JSON({surface = ev.surface,
782                                                    visible = 0})
783                                  wmgr:window_request(r,a,0)
784
785                              elseif event == "create" then
786
787                                 if verbose > 0 then
788                                     print("*** screen resource event: " ..
789                                           tostring(ev))
790                                 end
791
792                                 local regulation = mdb.select.select_driving_mode.single_value
793
794                                 if regulation == 1 then
795
796                                     local blacklisted = false
797
798                                     -- applications which have their category set to "entertainment"
799                                     -- or "undefined" are blacklisted, meaning they should be regulated
800
801                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
802                                         if v.appid == ev.appid then
803                                             if verbose > 0 then
804                                                 print(ev.appid .. " was blacklisted (undefined)")
805                                             end
806                                             blacklisted = true
807                                             break
808                                         end
809                                     end
810
811                                     if not blacklisted then
812                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
813                                             if v.appid == ev.appid then
814                                                 if verbose > 0 then
815                                                     print(ev.appid .. " was blacklisted (entertainment)")
816                                                 end
817                                                 blacklisted = true
818                                                 break
819                                             end
820                                         end
821                                     end
822
823                                     -- our local application config, which takes precedence
824                                     local conf = getApplication(ev.appid)
825
826                                     -- disable only non-whitelisted applications
827                                     if not conf or conf.resource_class == "player" then
828                                         if blacklisted then
829                                             if verbose > 0 then
830                                                 print("disabling screen for " .. ev.appid)
831                                             end
832                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
833                                         end
834                                     end
835                                 end
836
837                              elseif event == "destroy" then
838                                if verbose > 0 then
839                                     print("*** screen resource event: " ..
840                                           tostring(ev))
841                                  end
842                              else
843                                  if verbose > 0 then
844                                     print("*** screen resource event: " ..
845                                           tostring(ev))
846                                  end
847                              end
848                          end,
849   audio_event_handler = function(self, ev)
850                              local event = ev.event
851                              local appid = ev.appid
852                              local audioid = ev.audioid
853
854                              if event == "grant" then
855                                  if verbose > 0 then
856                                     print("*** grant audio to "..appid..
857                                           " ("..audioid..") in '" ..
858                                           ev.zone .. "' zone")
859                                  end
860                              elseif event == "revoke" then
861                                  if verbose > 0 then
862                                     print("*** revoke audio from "..appid..
863                                           " ("..audioid..") in '" ..
864                                           ev.zone .. "' zone")
865                                  end
866                              else
867                                  if verbose > 0 then
868                                     print("*** audio resource event: " ..
869                                           tostring(ev))
870                                  end
871                              end
872                         end
873 }
874
875 resclnt = resource_client {}
876
877 wmgr = window_manager {
878   geometry = function(self, w,h, v)
879                   if type(v) == "function" then
880                       return v(w,h)
881                   end
882                   return v
883              end,
884
885   application = function(self, appid)
886                      if appid then
887                          local app = application_lookup(appid)
888                          if not app then
889                              app = application_lookup("default")
890                          end
891                          return app
892                      end
893                      return { privileges = {screen="none", audio="none"} }
894                 end,
895
896   output_order = { 1, 0 },
897
898   outputs = { { name  = "Mid",
899                 id    = 0,
900                 zone  = "driver",
901                 areas = { Full = {
902                               id     = 20,
903                               pos_x  = 0,
904                               pos_y  = 0,
905                               width  = function(w,h) return w end,
906                               height = function(w,h) return h end
907                           },
908                           Left = {
909                               id     = 21,
910                               pos_x  = 0,
911                               pos_y  = 0,
912                               width  = 320,
913                               height = function(w,h) return h end
914                           },
915                           Right = {
916                               id     = 22,
917                               pos_x  = function(w,h) return w-320 end,
918                               pos_y  = 0,
919                               width  = 320,
920                               height = function(w,h) return h end
921                           }
922                         }
923                },
924                { name  = "Center",
925                  id    = 1,
926                  zone  = "driver",
927                  areas = { Status = {
928                               id     = 0,
929                               pos_x  = 0,
930                               pos_y  = 0,
931                               width  = function(w,h) return w end,
932                               height = 64
933                            },
934                            Full = {
935                               id     = 1,
936                               pos_x  = 0,
937                               pos_y  = 64,
938                               width  = function(w,h) return w end,
939                               height = function(w,h) return h-64-128 end
940                            },
941                            Upper = {
942                               id     = 2,
943                               pos_x  = 0,
944                               pos_y  = 64,
945                               width  = function(w,h) return w end,
946                               height = function(w,h) return (h-64-128)/2 end
947                            },
948                            Lower = {
949                               id     = 3,
950                               pos_x  = 0,
951                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
952                               width  = function(w,h) return w end,
953                               height = function(w,h) return (h-64-128)/2 end
954                            },
955                            UpperLeft = {
956                               id     = 4,
957                               pos_x  = 0,
958                               pos_y  = 64,
959                               width  = function(w,h) return w/2 end,
960                               height = function(w,h) return (h-64-128)/2 end
961                            },
962                            UpperRight = {
963                               id     = 5,
964                               pos_x  = function(w,h) return w/2 end,
965                               pos_y  = 64,
966                               width  = function(w,h) return w/2 end,
967                               height = function(w,h) return (h-64-128)/2 end
968                            },
969                            LowerLeft = {
970                               id     = 6,
971                               pos_x  = 0,
972                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
973                               width  = function(w,h) return w/2 end,
974                               height = function(w,h) return (h-64-128)/2 end
975                            },
976                            LowerRight = {
977                               id     = 7,
978                               pos_x  = function(w,h) return w/2 end,
979                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
980                               width  = function(w,h) return w/2 end,
981                               height = function(w,h) return (h-64-128)/2 end
982                            },
983                            SysApp = {
984                               id     = 8,
985                               pos_x  = 0,
986                               pos_y  = 64,
987                               width  = function(w,h) return w end,
988                               height = function(w,h) return h-64-128 end
989                            },
990                            ["SysApp.Left"] = {
991                               id     = 9,
992                               pos_x  = 0,
993                               pos_y  = 64,
994                               width  = function(w,h) return w/2-181 end,
995                               height = function(w,h) return h-64-128 end
996                            },
997                            ["SysApp.Right"] = {
998                               id     = 10,
999                               pos_x  = function(w,h) return w/2+181 end,
1000                               pos_y  = 64,
1001                               width  = function(w,h) return w/2-181 end,
1002                               height = function(w,h) return h-64-128 end
1003                            },
1004                            Control = {
1005                               id     = 11,
1006                               pos_x  = 0,
1007                               pos_y  = function(w,h) return h-128 end,
1008                               width  = function(w,h) return w end,
1009                               height = 128
1010                            },
1011                         }
1012               }
1013   },
1014   layers = { {      0, "Background"   , 1 },
1015              {      1, "Application"  , 2 },
1016              {      2, "Softkeyboard" , 4 },
1017              {      3, "HomeScreen"   , 2 },
1018              {      4, "ControlBar"   , 2 },
1019              {      5, "InterruptApp" , 2 },
1020              {      6, "OnScreen"     , 2 },
1021              {    101, "Input"        , 3 },
1022              {    102, "Cursor"       , 5 },
1023              {    103, "Startup"      , 6 },
1024              { 0x1000, "Background"   , 1 },
1025              { 0x2000, "Normal"       , 2 },
1026              { 0x3000, "Fullscreen"   , 2 },
1027              { 0x4000, "InputPanel"   , 3 },
1028              { 0xA000, "Touch"        , 4 },
1029              { 0xB000, "Cursor"       , 5 },
1030              { 0xC000, "Startup"      , 6 }
1031   },
1032
1033
1034   manager_update = function(self, oper)
1035                        if verbose > 0 then
1036                            print("### <== WINDOW MANAGER UPDATE:" ..
1037                                  window_manager_operation_name(oper))
1038                        end
1039                        if oper == 1 then
1040                            local wumask = window_mask { raise   = true,
1041                                                         visible = true,
1042                                                         active  = true }
1043                            local wrmask = window_mask { active  = true }
1044                            local lumask = layer_mask  { visible = true }
1045                            local lrmask = layer_mask  { visible = true }
1046                            local req = m:JSON({
1047                                passthrough_window_update  = wumask:tointeger(),
1048                                passthrough_window_request = wrmask:tointeger(),
1049                                passthrough_layer_update   = lumask:tointeger(),
1050                                passthrough_layer_request  = lrmask:tointeger()
1051                            })
1052                            self:manager_request(req)
1053                        end
1054                    end,
1055
1056   window_update = function(self, oper, win, mask)
1057                       if verbose > 0 then
1058                           print("### <== WINDOW UPDATE oper:" ..
1059                                 window_operation_name(oper) ..
1060                                 " mask: " .. tostring(mask))
1061                           if verbose > 1 then
1062                               print(win)
1063                           end
1064                       end
1065
1066                       local arg = m:JSON({ surface = win.surface,
1067                                            winname = win.name,
1068                       })
1069                       local command = 0
1070
1071                       if oper == 1 then -- create
1072                            local layertype = win.layertype
1073                            if layertype and input_layer[layertype] then
1074                                if verbose > 0 then
1075                                    print("ignoring input panel creation")
1076                                end
1077                                return
1078                            end
1079                            command     = 0x10001
1080                       elseif oper == 2 then -- destroy
1081                            command     = 0x10002
1082                       elseif oper == 3 then  -- namechange
1083                            command     = 0x10009
1084                       elseif oper == 4 or oper == 5 then --visible or configure
1085                            command     = 0x10008
1086                            arg.zone    = win.area
1087                            arg.node    = win.node
1088                            arg.layer   = win.layer
1089                            arg.pos_x   = win.pos_x
1090                            arg.pos_y   = win.pos_y
1091                            arg.width   = win.width
1092                            arg.height  = win.height
1093                            arg.raise   = win.raise
1094                            arg.visible = win.visible
1095                            arg.active  = win.active
1096                       elseif oper == 6 then -- active
1097                            command     = 0x10006
1098                            arg.active  = win.active
1099                       else
1100                            if verbose > 0 then
1101                                print("### nothing to do")
1102                            end
1103                            return
1104                       end
1105
1106                       local msg = m:JSON({ command = command,
1107                                            appid   = win.appid,
1108                                            pid     = win.pid,
1109                                            arg     = arg
1110                       })
1111                       if verbose > 0 then
1112                           print("### <== sending " ..
1113                                 command_name(msg.command) ..
1114                                 " window message to '" .. win.name .. "'")
1115                           if verbose > 1 then
1116                               print(msg)
1117                           end
1118                       end
1119                       sc:send_message(homescreen, msg)
1120
1121                       if oper == 1 then -- create
1122                           local i = input_layer[win.layertype]
1123                           local p = self:application(win.appid)
1124                           local s = p.privileges.screen
1125
1126                           if s == "system" then
1127                               local a = animation({})
1128                               local r = m:JSON({surface = win.surface,
1129                                                 visible = 1,
1130                                                 raise   = 1})
1131                               self:window_request(r,a,0)
1132                           else
1133                               if i then
1134                                   if verbose > 0 then
1135                                       print("do not make resource for " ..
1136                                             "input window")
1137                                   end
1138                               else
1139                                   resclnt:resource_set_create("screen",
1140                                                                "driver",
1141                                                                win.appid,
1142                                                                win.surface)
1143                                   special_screen_sets[win.surface] = true
1144                               end
1145                           end
1146                       elseif oper == 2 then -- destroy
1147                           resclnt:resource_set_destroy("screen", win.surface)
1148                           special_screen_sets[win.surface] = nil
1149                       elseif oper == 6 then -- active
1150                           if win.active then
1151                               local i = input_layer[win.layertype]
1152                               local p = self:application(win.appid)
1153                               local s = p.privileges.screen
1154                               local surface = win.surface
1155                               if not i and s ~= "system" then
1156                                  resclnt:resource_set_acquire("screen",surface)
1157                                  resmgr:window_raise(win.appid, surface, 1)
1158                               end
1159                           end
1160                       end
1161                   end,
1162
1163   layer_update = function(self, oper, layer, mask)
1164                       if verbose > 0 then
1165                           print("### LAYER UPDATE:" ..
1166                                 layer_operation_name(oper) ..
1167                                 " mask: " .. tostring(mask))
1168                           if verbose > 1 then
1169                               print(layer)
1170                           end
1171                       end
1172                       if oper == 3 then -- visible
1173                          local command = 0x10022
1174                          local msg = m:JSON({
1175                                          command = command,
1176                                          appid = "",
1177                                          arg = m:JSON({layer = layer.id,
1178                                                        visible = layer.visible
1179                                          })
1180                                 })
1181                          if verbose > 0 then
1182                             print("### <== sending "..command_name(command)..
1183                                   " layer message")
1184                             if verbose > 1 then
1185                                 print(msg)
1186                             end
1187                          end
1188                          sc:send_message(homescreen, msg)
1189                       else
1190                            if verbose > 0 then
1191                                print("### nothing to do")
1192                            end
1193                       end
1194                  end,
1195
1196   output_update = function(self, oper, out, mask)
1197                       local idx = out.index
1198                       local defidx = self.output_order[idx+1]
1199                       if verbose > 0 then
1200                           print("### OUTPUT UPDATE:" .. oper ..
1201                                 " mask: "..tostring(mask))
1202                       end
1203                       if not defidx then
1204                           return
1205                       end
1206                       print(out)
1207                       local outdef = self.outputs[defidx+1]
1208                       if (oper == 1) then -- create
1209                           if outdef then
1210                               self:output_request(m:JSON({index = idx,
1211                                                           id    = outdef.id,
1212                                                           name  = outdef.name
1213                                                           }))
1214                           end
1215                       elseif (oper == 5) then -- done
1216                           local ads = outdef.areas
1217                           local on = outdef.name
1218                           if ads then
1219                               for name,ad in pairs(ads) do
1220                                   local can = wmgr:canonical_name(on.."."..name)
1221                                   local a = m:JSON({name   = name,
1222                                                     output = out.index})
1223                                   for fld,val in pairs(ad) do
1224                                       a[fld] = self:geometry(out.width,
1225                                                              out.height,
1226                                                              val)
1227                                    end
1228                                    self:area_create(a)
1229                                    resmgr:area_create(area[can], outdef.zone)
1230                               end
1231                           end
1232                       end
1233                   end
1234 }
1235
1236
1237 imgr = input_manager {
1238   inputs = {{ name = "G27 Racing Wheel",
1239               id = 0,
1240               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1241                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1242                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1243                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1244              }}
1245   },
1246
1247   manager_update = function(self, oper)
1248                        if verbose > 0 then
1249                            print("### <== INPUT MANAGER UPDATE:" ..
1250                                  input_manager_operation_name(oper))
1251                        end
1252                    end,
1253
1254   input_update = function(self, oper, inp, mask)
1255                      if verbose > 0 then
1256                          print("### INPUT UPDATE:" ..
1257                                 input_operation_name(oper) ..
1258                                 " mask: " .. tostring(mask))
1259                           if verbose > 1 then
1260                               print(inp)
1261                           end
1262                       end
1263                  end,
1264   code_update  = function(self, oper, code, mask)
1265                      if verbose > 0 then
1266                          print("### CODE UPDATE: mask: " .. tostring(mask))
1267                          if verbose > 1 then
1268                              print(code)
1269                          end
1270                      end
1271                      local msg = m:JSON({ command = 1,
1272                                           appid = "org.tizen.ico.homescreen",
1273                                           arg = m:JSON({ device = code.device,
1274                                                          time = code.time,
1275                                                          input = code.input,
1276                                                          code = code.id,
1277                                                          state = code.state
1278                                            })
1279                      })
1280                      if verbose > 0 then
1281                          print("### <== sending " ..
1282                                command_name(msg.command) ..
1283                                " input message")
1284                          if verbose > 1 then
1285                              print(msg)
1286                          end
1287                      end
1288                      sc:send_message(homescreen, msg)
1289                  end
1290 }
1291
1292 sc = m:get_system_controller()
1293
1294 -- resource sets
1295 sets = {}
1296
1297 -- special screen resource sets
1298 -- TODO: just rewrite screen resource handling to use regular resource API
1299
1300 special_screen_sets = {}
1301
1302 -- user manager
1303 um = m:UserManager()
1304
1305 connected = false
1306 homescreen = ""
1307 onscreen = ""
1308
1309 -- these shoud be before wmgr:connect() is called
1310 if verbose > 0 then
1311    print("====== creating applications ======")
1312 end
1313 application {
1314     appid          = "default",
1315     area           = "Center.Full",
1316     privileges     = { screen = "none", audio = "none" },
1317     resource_class = "player",
1318     screen_priority = 0
1319 }
1320
1321 application {
1322     appid           = "weston",
1323     area            = "Center.Full",
1324     privileges      = { screen = "system", audio = "none" },
1325     resource_class  = "implicit",
1326     screen_priority = 30
1327 }
1328
1329 application {
1330     appid           = "org.tizen.ico.homescreen",
1331     area            = "Center.Full",
1332     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1333     privileges      = { screen = "system", audio = "system" },
1334     resource_class  = "player",
1335     screen_priority = 20
1336 }
1337
1338 application {
1339     appid           = "org.tizen.ico.statusbar",
1340     area            = "Center.Status",
1341     privileges      = { screen = "system", audio = "none" },
1342     resource_class  = "player",
1343     screen_priority = 20
1344 }
1345
1346 application {
1347     appid           = "org.tizen.ico.onscreen",
1348     area            = "Center.Full",
1349     privileges      = { screen = "system", audio = "system" },
1350     resource_class  = "player",
1351     screen_priority = 20
1352 }
1353
1354 application {
1355     appid           = "org.tizen.ico.login",
1356     area            = "Center.Full",
1357     privileges      = { screen = "system", audio = "system" },
1358     resource_class  = "player",
1359     screen_priority = 20
1360 }
1361
1362 application {
1363     appid           = "org.tizen.ico.camera_left",
1364     area            = "Center.SysApp.Left",
1365     privileges      = { screen = "system", audio = "none" },
1366     requisites      = { screen = "blinker_left", audio = "none" },
1367     resource_class  = "player",
1368     screen_priority = 30
1369 }
1370
1371 application {
1372     appid           = "org.tizen.ico.camera_right",
1373     area            = "Center.SysApp.Right",
1374     privileges      = { screen = "system", audio = "none" },
1375     requisites      = { screen = "blinker_right", audio = "none" },
1376     resource_class  = "player",
1377     screen_priority = 30
1378 }
1379
1380 application {
1381     appid           = "net.zmap.navi",
1382     area            = "Center.Full",
1383     privileges      = { screen = "none", audio = "none" },
1384     resource_class  = "navigator",
1385     screen_priority = 30
1386 }
1387
1388 application {
1389     appid           = "GV3ySIINq7.GhostCluster",
1390     area            = "Mid.Full",
1391     privileges      = { screen = "none", audio = "none" },
1392     resource_class  = "system",
1393     screen_priority = 30
1394 }
1395
1396 if sc then
1397     sc.client_handler = function (self, cid, msg)
1398         local command = msg.command
1399         local appid = msg.appid
1400         if verbose > 0 then
1401             print('### ==> client handler:')
1402             if verbose > 1 then
1403                 print(msg)
1404             end
1405         end
1406
1407         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1408         -- disconnection
1409
1410         if command == 0xFFFF then
1411             if verbose > 1 then
1412                 print('client ' .. cid .. ' (' .. msg.appid .. ') disconnected')
1413             end
1414             if msg.appid == homescreen then
1415                 if verbose > 1 then
1416                 end
1417                 connected = false
1418                 homescreen = ""
1419                 wmgr:disconnect()
1420                 for i,v in pairs(special_screen_sets) do
1421                     resource_set_destroy("screen", i)
1422                     special_screen_sets[i] = nil
1423                 end
1424             end
1425             return
1426         end
1427
1428         -- handle the connection
1429
1430         if not connected and appid then
1431             if appid == "org.tizen.ico.homescreen" then
1432                 print('Setting homescreen='..appid)
1433                 homescreen = appid
1434                 if command and command == 1 then
1435                     send_driving_mode_to_home_screen()
1436                     send_night_mode_to_home_screen()
1437                 end
1438                 print('Trying to connect to wayland...')
1439                 connected = wmgr:connect()
1440             elseif appid == "org.tizen.ico.onscreen" then
1441                 onscreen = appid
1442             end
1443         end
1444     end
1445
1446     sc.generic_handler = function (self, cid, msg)
1447         if verbose > 0 then
1448             print('### ==> generic handler:')
1449             if verbose > 1 then
1450                print(msg)
1451             end
1452         end
1453     end
1454
1455     sc.window_handler = function (self, cid, msg)
1456         if verbose > 0 then
1457             print('### ==> received ' ..
1458                    command_name(msg.command) .. ' message')
1459             if verbose > 1 then
1460                 print(tostring(msg))
1461             end
1462         end
1463
1464         local a = animation({})
1465         local nores = false
1466         if msg.command == 0x10003 then       -- ico SHOW command
1467             local raise_mask = 0x01000000
1468             local lower_mask = 0x02000000
1469             local nores_mask = 0x40000000
1470             local time_mask  = 0x00ffffff
1471             msg.arg.visible = 1
1472             if msg.arg then
1473                 local time = 200
1474                 if  msg.arg.anim_time then
1475                     local t = msg.arg.anim_time
1476                     time = m:AND(t, time_mask)
1477                     nores = not m:AND(t, nores_mask)
1478                     if m:AND(t, raise_mask) then
1479                         msg.arg.raise = 1
1480                     elseif m:AND(t, lower_mask) then
1481                         msg.arg.raise = 0
1482                     end
1483                 end
1484                 if msg.arg.anim_name then
1485                     a.show = { msg.arg.anim_name, time }
1486                     print('time: ' .. tostring(a.show[2]))
1487                 end
1488             end
1489             if not nores then
1490                 local p = wmgr:application(msg.appid)
1491                 local s = p.privileges.screen
1492                 if s == "system" then
1493                     nores = true
1494                     if not msg.arg.raise then
1495                         msg.arg.raise = 1
1496                     end
1497                 end
1498             end
1499             if verbose > 2 then
1500                 print('### ==> SHOW')
1501                 print(tostring(msg.arg))
1502             end
1503             if nores then
1504                 wmgr:window_request(msg.arg, a, 0)
1505             else
1506                 local surface = msg.arg.surface
1507                 resclnt:resource_set_acquire("screen", surface)
1508                 resmgr:window_raise(msg.appid, surface, 1)
1509             end
1510         elseif msg.command == 0x10004 then   -- ico HIDE command
1511             local raise_mask = 0x01000000
1512             local lower_mask = 0x02000000
1513             local nores_mask = 0x40000000
1514             local time_mask  = 0x00ffffff
1515             msg.arg.visible = 0
1516             if msg.arg then
1517                 local time = 200
1518                 if msg.arg.anim_time then
1519                     local t = msg.arg.anim_time
1520                     time = m:AND(t, time_mask)
1521                     nores = not m:AND(t, nores_mask)
1522                 end
1523                 if msg.arg.anim_name then
1524                     a.hide = { msg.arg.anim_name, time }
1525                     print('hide animation time: ' .. tostring(a.hide[2]))
1526                 end
1527             end
1528             if not nores then
1529                 local p = wmgr:application(msg.appid)
1530                 local s = p.privileges.screen
1531                 if s == "system" then
1532                     nores = true
1533                     msg.arg.raise = 0
1534                 end
1535             end
1536             if verbose > 2 then
1537                 print('### ==> HIDE REQUEST')
1538                 print(tostring(msg.arg))
1539             end
1540             if nores then
1541                 wmgr:window_request(msg.arg, a, 0)
1542             else
1543                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1544             end
1545         elseif msg.command == 0x10005 then   -- ico MOVE
1546             if verbose > 2 then
1547                 print('### ==> MOVE REQUEST')
1548                 print(tostring(msg.arg))
1549             end
1550             if msg.arg.zone then
1551                 msg.arg.area = msg.arg.zone
1552             end
1553             wmgr:window_request(msg.arg, a, 0)
1554             -- TODO: handle if area changed
1555         elseif msg.command == 0x10006 then   -- ico ACTIVE
1556             if not msg.arg.active then
1557                 msg.arg.active = 3 -- pointer + keyboard
1558             end
1559             if verbose > 2 then
1560                 print('### ==> ACTIVE REQUEST')
1561                 print(tostring(msg.arg))
1562             end
1563             wmgr:window_request(msg.arg, a, 0)
1564         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1565             if verbose > 2 then
1566                 print('### ==> CHANGE_LAYER REQUEST')
1567                 print(tostring(msg.arg))
1568             end
1569             --[[
1570             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1571                 print("do not change layer for other than cursor or touch")
1572                 return
1573             end
1574             --]]
1575             wmgr:window_request(msg.arg, a, 0)
1576         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1577             local framerate = msg.arg.framerate
1578             if not framerate or framerate < 0 then
1579                 framerate = 0
1580             end
1581             msg.arg.map = 1
1582             if verbose > 2 then
1583                 print('### ==> MAP_THUMB REQUEST')
1584                 print(msg.arg)
1585                 print('framerate: '..framerate)
1586             end
1587             wmgr:window_request(msg.arg, a, framerate)
1588         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1589             msg.arg.map = 0
1590             if verbose > 2 then
1591                 print('### ==> UNMAP_THUMB REQUEST')
1592                 print(msg.arg)
1593             end
1594             wmgr:window_request(msg.arg, a, 0)
1595         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1596             msg.arg.visible = 1
1597             if verbose > 2 then
1598                 print('### ==> SHOW_LAYER REQUEST')
1599                 print(msg.arg)
1600             end
1601             wmgr:layer_request(msg.arg)
1602         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1603             msg.arg.visible = 0
1604             if verbose > 2 then
1605                 print('### ==> HIDE_LAYER REQUEST')
1606                 print(msg.arg)
1607             end
1608             wmgr:layer_request(msg.arg)
1609         end
1610     end
1611
1612     sc.input_handler = function (self, cid, msg)
1613         if verbose > 0 then
1614             print('### ==> input handler: ' .. command_name(msg.command))
1615             if verbose > 1 then
1616                 print(msg)
1617             end
1618         end
1619         if msg.command == 0x20001 then -- add_input
1620             msg.arg.appid = msg.appid
1621             if verbose > 2 then
1622                 print('### ==> ADD_INPUT REQUEST')
1623                 print(tostring(msg.arg))
1624             end
1625             imgr:input_request(msg.arg)
1626         elseif msg.command == 0x20002 then -- del_input
1627             msg.arg.appid = ''
1628             if verbose > 2 then
1629                 print('### ==> DEL_INPUT REQUEST')
1630                 print(tostring(msg.arg))
1631             end
1632             imgr:input_request(msg.arg)
1633         elseif msg.command == 0x20003 then -- send_input
1634         end
1635     end
1636
1637     sc.user_handler = function (self, cid, msg)
1638         if verbose > 0 then
1639             print('### ==> user handler: ' .. command_name(msg.command))
1640             if verbose > 1 then
1641                 print(msg)
1642             end
1643         end
1644
1645         if not um then
1646             print("User Manager not initialized")
1647             return
1648         end
1649
1650         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1651             print("command CHANGE_USER")
1652             if not msg.arg then
1653                 print("invalid message")
1654                 return
1655             end
1656
1657             username = msg.arg.user
1658             passwd = msg.arg.pass
1659
1660             if not username then
1661                 username = ""
1662             end
1663
1664             if not passwd then
1665                 passwd = ""
1666             end
1667
1668             success = um:changeUser(username, passwd)
1669
1670             if not success then
1671                 reply = m.JSON({
1672                     appid = msg.appid,
1673                     command = cmd
1674                 })
1675                 if sc:send_message(msg.appid, reply) then
1676                     print('*** sent authentication failed message')
1677                 else
1678                     print('*** failed to send authentication failed message')
1679                 end
1680             end
1681
1682         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1683             print("command GET_USERLIST")
1684             if not msg.appid then
1685                 print("invalid message")
1686                 return
1687             end
1688
1689             users, currentUser = um:getUserList()
1690
1691             if not users then
1692                 print("failed to get user list")
1693                 return
1694             end
1695
1696             nUsers = 0
1697
1698             for i,v in pairs(users) do
1699                 nUsers = nUsers + 1
1700             end
1701
1702             if not currentUser then
1703                 currentUser = ""
1704             end
1705
1706             if verbose > 1 then
1707                 print("current user: " .. currentUser)
1708                 print("user list:")
1709                 for i,v in pairs(users) do
1710                     print(v)
1711                 end
1712             end
1713
1714             reply = m.JSON({
1715                 appid = msg.appid,
1716                 command = cmd,
1717                 arg = m.JSON({
1718                     user_num = nUsers,
1719                     user_list = users,
1720                     user_login = currentUser
1721                 })
1722             })
1723
1724             if verbose > 1 then
1725                 print("### <== GetUserList reply: " .. tostring(reply))
1726             end
1727
1728             if sc:send_message(msg.appid, reply) then
1729                 print('*** reply OK')
1730             else
1731                 print('*** reply FAILED')
1732             end
1733
1734         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1735             print("command GET_LASTINFO")
1736             if not msg.appid then
1737                 print("invalid message")
1738                 return
1739             end
1740
1741             lastInfo = um:getLastInfo(msg.appid)
1742
1743             if not lastInfo then
1744                 print("failed to get last info for app" .. msg.appid)
1745                 return
1746             end
1747
1748             reply = m.JSON({
1749                 appid = msg.appid,
1750                 command = cmd,
1751                 arg = m.JSON({
1752                     lastinfo = lastinfo
1753                 })
1754             })
1755
1756             if sc:send_message(msg.appid, reply) then
1757                 print('*** reply OK')
1758             else
1759                 print('*** reply FAILED')
1760             end
1761
1762         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1763             print("command SET_LASTINFO")
1764             if not msg.arg or not msg.appid then
1765                 print("invalid message")
1766                 return
1767             end
1768
1769             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1770         end
1771     end
1772
1773     sc.resource_handler = function (self, cid, msg)
1774         if verbose > 0 then
1775             print('### ==> resource handler: ' .. command_name(msg.command))
1776             if verbose > 1 then
1777                 print(msg)
1778             end
1779         end
1780
1781         createResourceSet = function (ctl, client, msg)
1782             cb = function(rset, data)
1783                 print("> resource callback")
1784
1785                 -- type is either basic (0) or interrupt (1)
1786                 requestType = 0
1787                 if msg.res.type then
1788                     requestType = msg.res.type
1789                 end
1790
1791                 if rset.acquired then
1792                     cmd = 0x00040001 -- acquire
1793                 else
1794                     cmd = 0x00040002 -- release
1795                 end
1796
1797                 reply = m.JSON({
1798                         appid = data.client,
1799                         command = cmd,
1800                         res = {
1801                             type = requestType
1802                         }
1803                     })
1804
1805                 if rset.resources.audio_playback then
1806                     reply.res.sound = {
1807                         zone = "driver",
1808                         name = msg.appid,
1809                         adjust = 0,
1810                         -- id = "0"
1811                     }
1812                 end
1813
1814                 if rset.resources.display then
1815                     reply.res.window = {
1816                         zone = "driver",
1817                         name = msg.appid,
1818                         -- id = "0"
1819                     }
1820                 end
1821
1822                 if rset.resources.input then
1823                     reply.res.input = {
1824                         name = msg.appid,
1825                         event = 0
1826                     }
1827                 end
1828                 print("sending message to client: " .. data.client)
1829
1830                 if sc:send_message(data.client, reply) then
1831                     print('*** reply OK')
1832                 else
1833                     print('*** reply FAILED')
1834                 end
1835             end
1836
1837             rset = m:ResourceSet({
1838                     application_class = "player",
1839                     zone = "driver", -- msg.zone ("full")
1840                     callback = cb
1841                 })
1842
1843             rset.data = {
1844                 cid = cid,
1845                 ctl = ctl
1846             }
1847
1848             if msg.res.sound then
1849                 rset:addResource({
1850                         resource_name = "audio_playback"
1851                     })
1852                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1853                 rset.resources.audio_playback.attributes.appid = msg.appid
1854                 print("sound name: " .. msg.res.sound.name)
1855                 print("sound zone:" .. msg.res.sound.zone)
1856                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1857                 if msg.res.sound.id then
1858                     print("sound id: " .. msg.res.sound.id)
1859                 end
1860             end
1861
1862             if msg.res.input then
1863                 rset:addResource({
1864                         resource_name = "input"
1865                     })
1866                 rset.resources.input.attributes.pid = tostring(msg.pid)
1867                 rset.resources.input.attributes.appid = msg.appid
1868                 print("input name: " .. msg.res.sound.name)
1869                 print("input event:" .. tostring(msg.res.input.event))
1870             end
1871
1872             if msg.res.window then
1873                 rset:addResource({
1874                         resource_name = "display"
1875                     })
1876                 rset.resources.display.attributes.pid = tostring(msg.pid)
1877                 rset.resources.display.attributes.appid = msg.appid
1878                 print("display name: " .. msg.res.display.name)
1879                 print("display zone:" .. msg.res.display.zone)
1880                 if msg.res.display.id then
1881                     print("display id: " .. msg.res.display.id)
1882                 end
1883             end
1884
1885             return rset
1886         end
1887
1888         -- parse the message
1889
1890         -- fields common to all messages:
1891         --      msg.command
1892         --      msg.appid
1893         --      msg.pid
1894
1895         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1896             print("command CREATE")
1897
1898             if not sets.cid then
1899                 sets.cid = createResourceSet(self, cid, msg)
1900             end
1901
1902         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1903             print("command DESTROY")
1904
1905             if sets.cid then
1906                 sets.cid:release()
1907             end
1908
1909             sets.cid = nil -- garbage collecting
1910
1911         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1912             print("command ACQUIRE")
1913
1914             if not sets.cid then
1915                 sets.cid = createResourceSet(self, cid, msg)
1916             end
1917
1918             sets.cid:acquire()
1919
1920         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1921             print("command RELEASE")
1922
1923             if sets.cid then
1924                 sets.cid:release()
1925             end
1926
1927         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1928             print("command DEPRIVE")
1929
1930         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1931             print("command WAITING")
1932
1933         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1934             print("command REVERT")
1935         end
1936     end
1937
1938     sc.inputdev_handler = function (self, cid, msg)
1939         if verbose > 0 then
1940             print('*** inputdev handler: ' .. command_name(msg.command))
1941             if verbose > 1 then
1942                 print(msg)
1943             end
1944         end
1945     end
1946 end
1947
1948 function send_driving_mode_to_home_screen()
1949     if homescreen == "" then
1950         return
1951     end
1952
1953     local driving_mode = mdb.select.select_driving_mode.single_value
1954
1955     if not driving_mode then driving_mode = 0 end
1956
1957     local reply = m:JSON({ command = 0x60001,
1958                            arg     = m:JSON({ stateid = 1,
1959                                               state   = driving_mode
1960                                      })
1961                   })
1962
1963     if verbose > 0 then
1964         print("### <== sending " .. command_name(reply.command) .. " message")
1965         if verbose > 1 then
1966             print(reply)
1967         end
1968     end
1969
1970     sc:send_message(homescreen, reply)
1971 end
1972
1973 function send_night_mode_to_home_screen()
1974     if homescreen == "" then
1975         return
1976     end
1977
1978     local night_mode = mdb.select.select_night_mode.single_value
1979
1980     if not night_mode then night_mode = 0 end
1981
1982     local reply = m:JSON({ command = 0x60001,
1983                            arg     = m:JSON({ stateid = 2,
1984                                               state   = night_mode
1985                                      })
1986                   })
1987
1988     if verbose > 0 then
1989         print("### <== sending " .. command_name(reply.command) .. " message")
1990         if verbose > 1 then
1991             print(reply)
1992         end
1993      end
1994
1995      sc:send_message(homescreen, reply)
1996 end
1997
1998 -- we should have 'audio_playback' defined by now
1999 m:try_load_plugin('telephony')